package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    // 1. 创建学生对象数组
    private static ArrayList<Student> stus = new ArrayList<>();

    static {
        Student stu1 = new Student("heima001", "张三", "23", "1999-11-11");
        Student stu2 = new Student("heima002", "李四", "24", "2000-11-11");

        stus.add(stu1);
        stus.add(stu2);
    }
    @Override
    public boolean addStudent(Student stu) {
        stus.add(stu);
        return true;
    }

    public Student[] findAllStudent() {
        Student[] students = new Student[stus.size()];
        for (int i = 0; i < stus.size(); i++) {
            students[i] = stus.get(i);
        }
        return students;
    }

    public void deleteStudentById(String deId) {
        // 1. 查找id所在的索引位置
        int index = getIndex(deId);
        // 2. 将该索引位置使用null原宿进行覆盖
        stus.remove(index);
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            Student stu = stus.get(i);
            if (stu != null && stu.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newStu) {
        // 1. 查找updateId在容器中的索引
        int index = getIndex(updateId);
        // 2. 将该索引位置，使用新的学生对象替换
        stus.set(index, newStu);
    }
}
